$(document).ready(function($){
    if($('.flexslider').length>0) {
        $('.flexslider').flexslider({
            animation: "slide",
            controlNav: false,
            start: function(slider){
                $('body').removeClass('loading');
            }
        });
    }
    fotoramaSlide();
    initCart();
    initCommonFunction();
    sendContact();
});

function fotoramaSlide(){
    if(jQuery().fotorama!=undefined){
        fotoramaDiv = $('.fotorama').fotorama({
            width: 700,
            maxwidth: '100%',
            ratio: 16/9,
            allowfullscreen: true,
            nav: 'thumbs',
            captions: true,
            show: $(this).attr('data-start-index'),
        }).on('fotorama:show', function (e, fotorama, extra) {
            var option = selectedInSet.find('option[data-index='+fotorama.activeIndex + ']');
            selectedInSet.val(option.val());
            changeSelectedProduct(option);
        });
        var selectedInSet = $('select#product-temp-detail');
        selectedInSet.change(function(){
            var option = $(this).find('option:selected');
            fotoramaDiv.data('fotorama').show({index: option.attr('data-index'), time: 0});
        });
        $('#product-qty').change(function(){
            var option = selectedInSet.find('option:selected');
            calculateOrderValue(option);
        });
    }
}

function changeSelectedProduct(option){
    $('#product-id').val(option.val());
    $('#product-price').val(option.attr('data-price'));
    $('#order-temp-price').html(option.attr('data-price-format'));
    $('#order-temp-price-old').html(option.attr('data-price-old-format'));
    $('.product-description').addClass('hidden');
    $('.description-' + option.val()).removeClass('hidden');
    calculateOrderValue(option);
}

function calculateOrderValue(option){
    var val = $('#product-qty').val() * option.attr('data-price');
    if(!isNaN(val)){
        $('#order-temp-value').html(formatCurrency(val));
    } else {
        $('#order-temp-value').html('');
    }
}